package org.standard.commons.utils;

public class Getch2spell {
	private static String _fromencode_ = "gbk";
	private static String _toencode_ = "gbk";
	
	
	public static int compare(String str1, String str2) {
		int result = 0;
		String m_s1 = null;
		String m_s2 = null;
		try {
			m_s1 = new String(str1.getBytes(_fromencode_), _toencode_);
			m_s2 = new String(str2.getBytes(_fromencode_), _toencode_);
		} catch (Exception e) {
			return str1.compareTo(str2);
		}
		result = chinesecompareto(m_s1, m_s2);
		return result;
	}

	public static int getcharcode(String s) {
		if (s == null && s.equals(""))
			return -1;
		byte b[] = s.getBytes();
		int value = 0;
		for (int i = 0; i < b.length && i <= 2; i++){
			value = value * 100 + b[i];
		}
		return value;
	}

	public static int chinesecompareto(String s1, String s2) {
		int len1 = s1.length();
		int len2 = s2.length();
		int n = Math.min(len1, len2);
		for (int i = 0; i < n; i++) {
			int s1_code = getcharcode(s1.charAt(i) + "");
			int s2_code = getcharcode(s2.charAt(i) + "");
			if (s1_code * s2_code < 0)
				return Math.min(s1_code, s2_code);
			if (s1_code != s2_code)
				return s1_code - s2_code;
		}
		return len1 - len2;
	}

	public static String getbegincharacter(String res) {
		String a = res;
		String result = "";
		for (int i = 0; i < a.length(); i++) {
			String current = a.substring(i, i + 1);
			if (compare(current, "\u554a") < 0){
				result = result + current;
			}else if (compare(current, "\u554a") >= 0 && compare(current, "\u5ea7") <= 0){
				if (compare(current, "\u531d") >= 0){
					result = result + "z";
				}else if (compare(current, "\u538b") >= 0){
					result = result + "y";
				}else if (compare(current, "\u6614") >= 0){
					result = result + "x";
				}else if (compare(current, "\u6316") >= 0){
					result = result + "w";
				}else if (compare(current, "\u584c") >= 0){
					result = result + "t";
				}else if (compare(current, "\u6492") >= 0){
					result = result + "s";
				}else if (compare(current, "\u7136") >= 0){
					result = result + "r";
				}else if (compare(current, "\u671f") >= 0){
					result = result + "q";
				}else if (compare(current, "\u556a") >= 0){
					result = result + "p";
				}else if (compare(current, "\u54e6") >= 0){
					result = result + "o";
				}else if (compare(current, "\u62ff") >= 0){
					result = result + "n";
				}else if (compare(current, "\u5988") >= 0){
					result = result + "m";
				}else if (compare(current, "\u5783") >= 0){
					result = result + "l";
				}else if (compare(current, "\u5580") >= 0){
					result = result + "k";
				}else if (compare(current, "\u51fb") > 0){
					result = result + "j";
				}else if (compare(current, "\u54c8") >= 0){
					result = result + "h";
				}else if (compare(current, "\u5676") >= 0){
					result = result + "g";
				}else if (compare(current, "\u53d1") >= 0){
					result = result + "f";
				}else if (compare(current, "\u86fe") >= 0){
					result = result + "e";
				}else if (compare(current, "\u642d") >= 0){
					result = result + "d";
				}else if (compare(current, "\u64e6") >= 0){
					result = result + "c";
				}else if (compare(current, "\u82ad") >= 0){
					result = result + "b";
				}else if (compare(current, "\u554a") >= 0){
					result = result + "a";
				}
			}
		}
		return result;
	}

	public static String getfirststr(String str) {
		int strLength = str.length();
		String str1 = "";
		char aa[] = new char[strLength];
		for(int i =0;i<strLength;i++){
			aa[i] = str.charAt(i);
			String sss = new String(aa);
			if (isChineseChar(sss)){
				str1 = getbegincharacter(sss);
			}else{
				str1 = sss;
			}
		}
		
		return str1;
	}

	
	//3 .判断是否为汉字 

	public static boolean isChineseChar(String str){
	  
	    char[] chars=str.toCharArray(); 
	    boolean isGB2312=false; 
	    for(int i=0;i<chars.length;i++){
	                byte[] bytes=(""+chars[i]).getBytes(); 
	                if(bytes.length==2){ 
	                            int[] ints=new int[2]; 
	                            ints[0]=bytes[0]& 0xff; 
	                            ints[1]=bytes[1]& 0xff; 
	                            if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){ 
	                                        isGB2312=true; 
	                                        break; 
	                            } 
	                } 
	    } 
	    return isGB2312; 
	}

	public static void main(String[] args) {
		
		String temp = Getch2spell.getfirststr("欲");
		
		System.out.println(temp);
	}
}
